package com.ktjiaoyu.ktmall.servlet.api;

import com.alibaba.fastjson2.JSON;
import com.ktjiaoyu.ktmall.entity.Category;
import com.ktjiaoyu.ktmall.service.CategoryService;
import com.ktjiaoyu.ktmall.service.impl.CategoryServiceImpl;
import com.ktjiaoyu.ktmall.utils.R;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(name = "CategoryServlet",urlPatterns = "/api/category/*")
public class CategoryServlet extends HttpServlet {
    private final CategoryService categoryService=new CategoryServiceImpl();

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("application/json;charset=utf-8");

        String path=request.getPathInfo();
        R result;
        if ("/tree".equals(path)){
            List<Category> tree=categoryService.findTree();
            result= R.ok(tree);
        }else{
            List<Category> list=categoryService.findAll();
            result= R.ok(list);
        }
        PrintWriter out = response.getWriter();
        out.print(JSON.toJSONString(result));
        out.flush();
        out.close();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("application/json;charset=utf-8");

        BufferedReader reader = req.getReader();
        StringBuilder sb = new StringBuilder();
        String line;
        while ((line=reader.readLine())!=null){
            sb.append(line);
        }
        Category category=JSON.parseObject(sb.toString(),Category.class);
        boolean success;
        System.out.println(category.getId()
        );
        if (category.getId()==null) {
            success = categoryService.insert(category) > 0;
        }else {
            success = categoryService.update(category)>0;
        }
        R result = success?R.ok():R.fail();
        PrintWriter out = resp.getWriter();
        out.print(JSON.toJSONString(result));
        out.flush();
        out.close();
    }

    @Override
    protected void doDelete(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("application/json;charset=utf-8");


        String pathInfo = req.getPathInfo();
        Long id=null;
        if (pathInfo!=null&&pathInfo.length()>1){
            id=Long.valueOf(pathInfo.substring(1));
        }
        boolean success = id!=null&& categoryService.removeById(id);
        R result = success?R.ok():R.fail();
        PrintWriter out = resp.getWriter();
        out.print(JSON.toJSONString(result));
        out.flush();
        out.close();
    }
}
